/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Reminder.java
 *
 * Created on 05.12.2011., 11.52.23
 */
package tok.predmeta;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

/**
 *
 * @author mega
 */
public class Reminder extends javax.swing.JFrame {
    JPopupMenu menu = new JPopupMenu("Popup");
    static String Brpredmeta;

    /** Creates new form Reminder */
    public Reminder() {
        initComponents();
        jTextField1.requestFocus();
        jTextField1.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void changedUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                warn();
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                warn();
            }

            public void warn() {
                if (jTextField1.getText().equals("02")) {
                     Frame1 P = new Frame1();
                    dispose();
                    P.setVisible(true);
                } 
            }
        });
        jTextArea1.addMouseListener( new MouseAdapter(){
            private Object popup;
            @Override
     public void mousePressed(MouseEvent ev) {
        if (ev.isPopupTrigger()) {
          menu.show(ev.getComponent(), ev.getX(), ev.getY());
        }
      }

            @Override
      public void mouseReleased(MouseEvent ev) {
        if (ev.isPopupTrigger()) {
          menu.show(ev.getComponent(), ev.getX(), ev.getY());
        }
      }

        });
        
        JMenuItem item = new JMenuItem("КОПИРАЈ");
    item.addActionListener(new ActionListener() {
            @Override
      public void actionPerformed(ActionEvent e) {
        jTextField2.setText(jTextArea1.getSelectedText());
      }
    });
    menu.add(item);
        
        Calendar cal1 = new GregorianCalendar();
        Calendar cal2 = new GregorianCalendar();
        Calendar cal22 = new GregorianCalendar();
        GregorianCalendar gregorianCalendar = new GregorianCalendar();
        String month = String.valueOf(gregorianCalendar.get(GregorianCalendar.MONTH));
        int Month = Integer.parseInt(month);
        String day = String.valueOf(gregorianCalendar.get(GregorianCalendar.DAY_OF_MONTH));

        int Day = Integer.parseInt(day);
        String year = String.valueOf(gregorianCalendar.get(GregorianCalendar.YEAR));
        int Year = Integer.parseInt(year);




        // Load the JDBC driver
        String driverName = "com.mysql.jdbc.Driver"; // MySQL  JDBC driver
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);
        }

// Create a connection to the database
        String serverName = "192.168.88.10";
        String mydatabase = "tok_predmeta";
        String url = "jdbc:mysql://" + serverName + "/" + mydatabase + "?useUnicode=true&characterEncoding=utf8"; // a JDBC url
        String username = "tokpred";
        String password = "Lx3sAQdnYJFTxBhs";
        boolean start = false;

        try {
             java.sql.Connection connection = DriverManager.getConnection(url, username, password);
             java.sql.PreparedStatement s = connection.prepareStatement("SELECT *  FROM predmeti"); //LEFT JOIN state ON (predmeti.sifra=state.sifra) ");

            s.executeQuery();
            java.sql.PreparedStatement su = connection.prepareStatement("DELETE from predmeti2");
                    su.execute();

            ResultSet rs = s.getResultSet();

            ArrayList al = new ArrayList();
            int count = 0;
            while (rs.next()) {
                int idNum = rs.getInt("id");
                String sifra = rs.getString("sifra");
                  String korisnicka_sifra = rs.getString("korisnicka_sifra");
                String ime = rs.getString("ime");
                String prezime = rs.getString("prezime");
                String adresa = rs.getString("adresa");
                String jmbg = rs.getString("jmbg");
                String tip = rs.getString("tip");
                String datum = rs.getString("datum");
                //  String datum2 = rs.getString("date");
                String[] datum_predmeta = datum.split("/");



                int Dan = Integer.parseInt(datum_predmeta[0]);
                int Mesec = Integer.parseInt(datum_predmeta[1]) - 1;
                int Godina = Integer.parseInt(datum_predmeta[2]);
                int broj_dana2 = daysBetween(cal22.getTime(), cal1.getTime());
                broj_dana2 = Math.abs(broj_dana2);
                cal1.set(Year, Month, Day);
                cal2.set(Godina, Mesec, Dan);

                int broj_dana = daysBetween(cal2.getTime(), cal1.getTime());
                broj_dana = Math.abs(broj_dana);
                if (broj_dana > 0 && broj_dana < 25) {
                    al.add(" Шифра : " + sifra + " \n Име : " + ime + " \n Презиме : " + prezime + " \n Адреса: " + adresa + " \n ЈМБГ : " + jmbg + " \n Тип предмета : " + tip + " \n Датум пријема : " + datum + " \n"
                            + " Број дана од пријема : " + broj_dana + "\n"
                            + "--------------------------------------------------------------\n");
                }
                

                  if (broj_dana > 5 && broj_dana < 20 ) {

                    
                    java.sql.PreparedStatement e = connection.prepareStatement("INSERT INTO predmeti2 (sifra,korisnicka_sifra,ime,prezime,adresa,jmbg,tip,datum,korisnik) VALUES "
                                    + "(?,?,?,?,?,?,?,?,?)");

                            e.setString(1, sifra);
                            e.setString(2, korisnicka_sifra);
                            e.setString(3, ime);
                            e.setString(4, prezime);
                            e.setString(5, adresa);
                            e.setString(6, jmbg);
                            e.setString(7, tip);
                            e.setString(8, datum);
                             e.setString(9, Login.user);
                            e.executeUpdate();
                }




                count++;


            }



            String red = "СПИСАК ПРЕДМЕТА  : \n\n\n";
            Iterator<String> itr = al.iterator();
            while (itr.hasNext()) {

                red += itr.next() + "\n";

            }

            jTextArea1.setText(red);


            s.close();
            connection.close();

        } catch (SQLException ex) {
            Logger.getLogger(Pretraga.class.getName()).log(Level.SEVERE, null, ex);

        }

        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Arial", 0, 18));
        jLabel1.setText("ПОДСЕТНИК ");

        jTextArea1.setColumns(20);
        jTextArea1.setEditable(false);
        jTextArea1.setFont(new java.awt.Font("Arial", 0, 15));
        jTextArea1.setRows(5);
        jScrollPane1.setViewportView(jTextArea1);

        jButton1.setText("Назад");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Назад");

        jButton2.setText("Пронађи");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setText("За више информација о току захтева упишите шифру предмета и кликните на дугме \"Пронађи\"");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(31, 31, 31)
                            .addComponent(jButton2)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 634, Short.MAX_VALUE)
                            .addComponent(jLabel2)
                            .addGap(192, 192, 192))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(42, 42, 42)
                                    .addComponent(jButton1))
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 1067, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addContainerGap(57, Short.MAX_VALUE)))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 450, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addComponent(jLabel2))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                            .addComponent(jButton2))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Frame1 Fr = new Frame1();
        dispose();
        Fr.setVisible(true);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        Brpredmeta = jTextField2.getText();
       
        Detail D = new Detail();

         dispose();
        //this.setVisible(false);
        D.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Reminder().setVisible(false);
                Tabela t =new Tabela();
                t.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextArea1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables

    public final int daysBetween(Date d1, Date d2) {
        return (int) ((d1.getTime() - d2.getTime()) / (1000 * 60 * 60 * 24));
    }
}
